/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neuronalenetzeelearning.view.controlpanel;

import neuronalenetzeelearning.view.IController;
import neuronalenetzeelearning.view.IView;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import neuronalenetzeelearning.model.Graph;
import neuronalenetzeelearning.model.Layer1Node;
import neuronalenetzeelearning.model.Layer2Node;
import neuronalenetzeelearning.view.gui.GraphVisualization;
import prefuse.data.Node;

/**
 *
 * @author svenmaster
 */
public class GUITestPanels extends JFrame implements IView {

    private BorderLayout borderLayout;//Fenster wird in Himmelsrichtungen aufgeteilt
    private JPanel graphVizPanel;
    private JPanel rightPanel;
    private GraphVisualization graphViz;
    private Controller controller;
    private static HashMap<ControlPanelPage, GraphPanelProperty> panelList;

    public GUITestPanels(Graph g) {
        super("NNEL");

        panelList = new HashMap();
        panelList.put(ControlPanelPage.DescriptionGraphPanel, new DescriptionGraphPanel(g));
        panelList.put(ControlPanelPage.PropertyGraphPanel, new PropertyGraphPanel());
        panelList.put(ControlPanelPage.LearningRuleChooserPanel, new LearningRuleChooserPanel());
        panelList.put(ControlPanelPage.LearningRuleHebbPanel, new LearningRuleHebbPanel());
        panelList.put(ControlPanelPage.LearningRuleSuperwisedLearningPanel, new LearningRuleSuperwisedLearningPanel());
        panelList.put(ControlPanelPage.StepModeChooserPanel, new StepModeChooserPanel());
        panelList.put(ControlPanelPage.StepwisePanel, new StepwisePanel());
        panelList.put(ControlPanelPage.NStepPanel, new NStepPanel());
        panelList.put(ControlPanelPage.PassPanel, new PassPanel());

        //Set the look and feel
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//für die Darstellung der Buttons u.ä.
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GUITestPanels.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GUITestPanels.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUITestPanels.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(GUITestPanels.class.getName()).log(Level.SEVERE, null, ex);
        }

        //applying borderlayout
        borderLayout = new BorderLayout();
        this.setLayout(borderLayout);
        graphVizPanel = new JPanel();
        rightPanel = new JPanel();
        this.add(graphVizPanel, BorderLayout.CENTER);
        this.add(rightPanel, BorderLayout.EAST);

        //setting panel's layout
        rightPanel.setBackground(Color.WHITE);
        graphVizPanel.setBackground(Color.WHITE);

        rightPanel.add(panelList.get(ControlPanelPage.DescriptionGraphPanel));

        //adding viz to center panel
        graphViz = new GraphVisualization(g);
        graphVizPanel.add(graphViz.getVisualisationPanel());
        graphViz.startVisualization();


        //set properties
        this.setPreferredSize(new Dimension(800, 520));
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setBackground(Color.WHITE);
    }

    public GraphVisualization getGraphViz() {
        return graphViz;
    }

    public void setGraphViz(GraphVisualization graphViz) {
        this.graphViz = graphViz;
        this.validate();
    }

    @Override
    public JPanel getGraphVizPanel() {
        return graphVizPanel;
    }

    public void setGraphVizPanel(JPanel graphVizPanel) {
        this.graphVizPanel = graphVizPanel;
    }

    public JPanel getRightPanel() {
        return rightPanel;
    }

    public void setRightPanel(JPanel rightPanel) {
        this.rightPanel = rightPanel;
    }

    @Override
    public void addNode(neuronalenetzeelearning.model.Node node) {
        Node newNode = graphViz.getGraph().addNode();
        //set properties of newNode to the values corresponding to parameter
        newNode.set("threshold", String.valueOf(node.getThreshold()));
        newNode.set("name", node.getName());
        if (node instanceof Layer1Node) {
            newNode.set("layer", "Layer1Node");
        } else {
            if (node instanceof Layer2Node) {
                newNode.set("layer", "Layer2Node");
            }
        }
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Graph graph = Graph.getExampleGraph();
                GUITestPanels gui = new GUITestPanels(graph);
                Controller controller = new Controller(graph, gui, panelList);
            }
        });
    }

    @Override
    public void setController(IController controller) {
        this.controller = (Controller) controller;
    }

    @Override
    public IController getController() {
        return (IController) this.controller;
    }
}